home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 27 / CU Amiga Magazine's Super CD-ROM 27 (1998)(EMAP Images)(GB)[!][issue 1998-10].iso / GoodBye / JohnKennedy / December93.lha / SICKNESS.LHA / robot.doc < prev   
Encoding:
Text File  |  1993-10-04  |  12.1 KB  |  237 lines

  1. Words: 1800
  2. Pictures:  Sorry, there ain't no real good pictures.  There are
  3. no diagrams either - just one screen shot, and one table. A picture
  4. called LISA.IFF is included to demonstrate how the table should
  5. appear.
  6.  
  7. Lisa --
  8. Important
  9. ----------
  10.  
  11. This month's article requires that some programs appear on the coverdisk. The programs that must be included are:
  12.  
  13.         miniFORTH
  14.         program1
  15.         program2
  16.         program3
  17.         program4
  18.         program5
  19.         program6
  20.         program7
  21.  
  22. Combined, they take up very little space. I've assumed they live on the disk in a drawer called 'miniforth'. Please see that whoever puts the disk together gets these files. Thank you.
  23.  
  24.           
  25.  
  26. The Final Robot Article
  27. ------------------------
  28.  
  29. No more hardware - this month John Kennedy describes the specially written programming language which will bring your creations to life.
  30.  
  31.  
  32. Introduction
  33.  
  34.  
  35. Building a robot is all very well, but how do you get it to move? So far, the only way we have been able to communicate with the input/output port has been through some Comms software: as the port is connected to the serial port, it is relatively easy to type in commands directly.
  36.  
  37. Unfortunately, controlling the hardware in this way is far from perfect. It's impossible to get the robot to make decisions itself, or to react to inputs from touch or light sensors - in fact, it's about as much of a robot as a remote control car.
  38.  
  39. To provide some semblance at Artificial Intelligence, we need a decent programming language. It would be entirely possible to use the ARexx scripting language which comes with all Amigas, but I though I would take this opportunity to show you a language I've always had a soft spot for -
  40. FORTH.
  41.  
  42. FORTH was written in about 1970 by a fellow called Charles Moore. Chuck needed a language that was quick to develop in, used little memory and would control his Radio Telescope. He wanted a fourth generation language, but the operating system he was using only allowed five letter names - and
  43. thus FORTH was born.
  44.  
  45. Back in the 1980's a little plastic computer called the Jupiter Ace appeared and totally failed to popularise the language. Being compact, FORTH was the ideal choice for a severely memory limited computer, and compared the most popular language in those days (Sinclair BASIC), it was ludicrously fast.
  46.  
  47. Recently Real 3D version 2 has been enhanced to feature a built-in FORTH system, so it seems we'll never be able to shake off one of the simplest yet most powerful languages ever designed.
  48.  
  49.  
  50. FORTH PRINCIPLES
  51.  
  52. A stack is an area of memory which is used to temporarily store information, and FORTH makes heavy use of one. Even simple calculations require numbers so placed on a stack, so it's important that its operation is clearly understood. For starters, every number that appears in a FORTH program is immediately stacked, and even entering '20' at the keyboard will place a number on the stack. 
  53.  
  54. This makes passing data to functions quite simple. For example, the FORTH word for printing a number is a full-stop, so if you were to enter the line:
  55.  
  56. 20 .
  57.  
  58. you would see the number displayed on screen. The dot actually removes a number from the stack and performs an operation with it. Likewise, the FORTH word for addition, +, takes two numbers off the stack, adds them together and then stacks the sum. So, the line:
  59.  
  60. 1 2 + .
  61.  
  62. will print 3 on the screen.
  63.  
  64. The FORTH system which came on the coverdisk contains about 80 words, and the more common ones are shown in the table. You can get a list of them by using the FORTH word VLIST, which stands for 'vocabulary list'.
  65.  
  66. The words supplied offer a range of arithmetic and logic operations, decision and loop control, external hardware control, simple graphics and mouse and joystick support.
  67.  
  68.  
  69. USING MINIFORTH
  70.  
  71.  
  72. The best way to learn how to use a programming language is to use it, so here's how to get miniFORTH working. 
  73.  
  74. First of all, boot your Amiga from your normal Workbench disk. When all the clunking and whirring eventually stops, open a SHELL window. Now pop out your Workbench disk and insert the coverdisk.
  75.  
  76. We need to make the current directory the directory on the disk which contains the FORTH program and its example files, so enter the following at the prompt:
  77.  
  78. cd df0:miniFORTH (and return)
  79.  
  80. Now this assumes that the coverdisk has been produced in a particular way, and for various reasons that mightn't be the case. See the coverdisk pages for confirmation.
  81.  
  82. When you have got the current directory set, start the FORTH system by typing:
  83.  
  84. miniforth (and return again)
  85.  
  86.  
  87. You should be greeted by a small welcome message, and 'OK'. The 'OK' is FORTH's way of telling you that everything is hunky-dory. It will say 'OK' every time it recognises a word. For now, enter:
  88.  
  89. VLIST (and return yet again)
  90.  
  91. You should see a large collection of meaningless (for the moment) text pop up on the screen. And, of course, the word 'OK'.  Now for some arithmetic - try entering:
  92.  
  93. 1 2 + .
  94.  
  95. This should display 3. Similarly, 10 5 * . should display 50. For some tricky subtraction, you will need to understand the way in which the stack works, or you'll get your sums all wrong.
  96.  
  97. The FORTH stack is a 'first in - last out' stack. That means if I stacked the numbers 1,2 and 3 in that order, when I popped them off I would get 3,2 and 1. So in order to subtract 1 from 100, you need to enter:
  98.  
  99. 100 1 - .
  100.  
  101. And to divide 100 by 10, you enter:
  102.  
  103. 100 10 / .
  104.  
  105. Note that miniFORTH is an integer based language, so floating point fractions aren't allowed at present. The words 1+, 1-, 2+ and 2- are short cut words for the commonly met situation where a number needs to be quickly changed.
  106.  
  107. 100 2+ . 
  108.  
  109. will display 102, and is exactly the same (but microscopically faster) than 
  110. 100 2 + .
  111.  
  112. which makes use of '+'.
  113.  
  114.  
  115.  
  116. DIY WORDS
  117.  
  118. A FORTH program is a list of words, with each word making use of words previously defined. Although miniFORTH comes with about 80 words of its own, you'll probably want to write some new ones. Creating words is easy, and makes use of a colon (':') and a semi-colon (';'). For example, here's a FORTH definition of a word which does some arithmetic.
  119.  
  120.  
  121. : SUMS
  122.     1 1 + . CR
  123.     1 2 + . CR
  124.     1 3 + . CR
  125. ;
  126.  
  127.  
  128. The FORTH word CR tells the computer to take a new-line: it stands for carriage return. The word SUMS has already been entered for you, and is stored on the coverdisk in the same directory as the program. To load it, type 
  129.  
  130. load program1
  131.  
  132. If all goes well, the familiar 'OK' should appear.  Now do a vocabulary list (enter VLIST) and you should notice a new word right at the end.  To find out more about it, enter VLISTFULL which will list its contents.  If should look like our definition above, but with some more comments in round brackets.  To execute this word simply type its name, so enter SUMS at the keyboard.  As if by magic, the answers should appear.
  133.  
  134. If you want to look at the program as I entered it, it is a plain ASCII text file, so you can either display it with TYPE from the CLI, or by loading it into any word processor.  You'll notice a lot of comments between round brackets - these are ignored by the FORTH system, but they are there for your benefit. Check all the other example programs for helpful information on what is going on!
  135.  
  136. The FORTH word LOAD takes a text file stored on disk, and treats it as though it were entered at the keyboard. To create your own words you will need to use a text editor to create a file, and then use miniFORTH to LOAD it in. 
  137.  
  138. Check through the other example programs.  Program3 is full of details on how to make use of variables and constants in miniFORTH (fun!) and Program4 demonstrates the graphical features of miniFORTH (rather slowly, I'm afraid).  It also shows how to make a program autostart when your load it (by the way, type END to leave miniFORTH).
  139.  
  140. Program5 is a primitive paint program (click the mouse button to stop it). Program6 is the robot control program, but before you can use it there are still a few details that need dealt with.
  141.  
  142.  
  143. ROBOTS ARE GO
  144.  
  145. If you remember, the input/output port expects serial data at a baud rate of 1200 baud maximum. In order to communicate with it, the Amiga must be talking at the right speed. To set the speed, find the Prefs drawer on your Workbench, and find the serial tool inside it. When you run this program,
  146. you can select the speed that you wish the serial port to run at, so make sure it's set at 1200. After you hit save you can be sure that when you run miniFORTH it will talk to the robot at the right speed.
  147.  
  148. There are six words which communicate with the input/output port, and they correspond exactly the six control words which are detailed in the input/output port's instructions. For example CONFIGUREBYTE will take a number off the stack, and use it to set the Ins and Outs of the port. See the words defined in Program6 for more details. Note that if there isn't a working device attached to the serial port when the robot commands are given, the system will hang. This is because it is expecting the acknowledge signals from the port, so don't run it unless you do have the robot hardware present.
  149.  
  150.  
  151. FURTHER IDEAS
  152.  
  153. Now that we have a full language to play with, our robot can start to think for itself. The example control program will only drive the buggy around in a square, but with input sensors you should be able to construct quite a complicated system, capable of finding its way around mazes and obstacles.
  154.  
  155.  
  156.  
  157. THE END
  158.  
  159. So that's the end of our Robot project. I hope you've found it useful, for not only have we looked at ways of interfacing just about anything to your Amiga, but we have also created a programming language especially to do it with.
  160.  
  161.  
  162. Next month
  163.     Next month's DIY will be a retrospective of past projects, as I take a look back at some of the most common pitfalls (mostly all mine) which have befallen budding DIYers. Don't miss it!
  164.  
  165. { Lisa - better check this with DAN, he might be expecting
  166. the PARNET project. If he is, this is no problem, as long as
  167. I am told! }
  168.  
  169.  
  170.  
  171. WORD DEFINITIONS
  172.  
  173. {Lisa, in this table the entries are separated by a row of
  174. dashes. The items on each line are separated by a two
  175. slashes (//). I know the tables usually get mucked up,
  176. so I hope this helps. I know it won't, so I've included a
  177. screen shot called LISA.IFF in this archive. This screen
  178. shot is not for the magazine, rather just for you to look at
  179. to get the table right. Good luck.}
  180.  
  181.  
  182. Here is a list of some of the words supplied with the
  183. miniFORTH system. For more details please check out the
  184. example programs. If you are keen to use this language, I
  185. recommend you get hold of a good library book on the
  186. subject.
  187.  
  188.  
  189. FORTH WORD      //      NAME    //  DESCRIPTION
  190.  
  191. ---------------------------------------------------
  192. .               //      Dot     //  Take a number off the stack and display it.
  193.  
  194. ---------------------------------------------------
  195.  
  196. ."              //      DotQuote    // Display text. Text will have been previously entered between square brackets
  197.  
  198. ---------------------------------------------------
  199.  
  200. VLIST           //      vocabulary list // List all words currently in dictionary
  201.  
  202. ---------------------------------------------------
  203.  
  204. VLISTFULL       //      full list   // List all user-defined words in detail
  205.  
  206. ---------------------------------------------------
  207.  
  208. +,-,*,/         //      Arithmetic operations // Remove numbers from stack and put the answer back
  209.  
  210. ---------------------------------------------------
  211.  
  212. =               //  Equals  //  Take two numbers off the stack. If they are equal, stack a 1 else a 0.
  213.  
  214. ---------------------------------------------------
  215.  
  216. IF/THEN         // If clause    // Take a number off the stack. If it is 1, perform the words between the IF and the ELSE
  217.  
  218. ---------------------------------------------------
  219.  
  220. DO/LOOP         // Loop //  Take two numbers off the stack, and repeat the contents of the loop.
  221.  
  222. ---------------------------------------------------
  223.  
  224. END             // End  // Leave the miniFORTH system
  225.  
  226. ---------------------------------------------------
  227.  
  228. LOAD <filename>     // Load // Load new words from a disk file
  229.  
  230. ---------------------------------------------------
  231.  
  232.  
  233.  
  234. Caption:
  235.  
  236. robot.iff
  237.         If you've got the miniFORTH program working, and you enter VLIST, this is the sort of display you should be greeted with.